package com.hdd.hll.common.response;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @Description:  在拦截器中 直接返回  json 数据
 * @author: coco
 * @date: 2019-10-29 10:43:46
 */
public class ResponseIo {

    private static Logger logger = LoggerFactory.getLogger(ResponseIo.class);


    public static  void responseOut(HttpServletResponse response,
                                       Object obj) {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setStatus(422);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer=null;
        try {
            writer=response.getWriter();
            writer.write(JSONObject.toJSONString(obj));
            writer.flush();
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }finally {
            if(writer!=null) {
                writer.close();
            }
        }
    }




}
